" Clean up trailing whitespaces {{{1
function! ClearWhitespaceFunction()
  " set up a marker
  normal mp
  " clear whitespace
  %s/\s\+$//e
  " return to the previous marker
  normal `p
endfunction

command! ClearWhitespace silent! call ClearWhitespaceFunction()

" Clean up trailing whitespaces {{{1
function! ClearContinuousEmptyLinesFunction()
  " set up a marker
  normal mp
  " clear continuous empty lines
  %!cat -s
  " return to the previous marker
  normal `pzz
endfunction
" Clean up continuous empty lines
command! ClearContinuousEmptyLines silent! call ClearContinuousEmptyLinesFunction()

" Clean up two continuous spaces in the middle of a line
function! ClearTwoContinuousSpacesFunction()
  " set up a marker
  normal mp
  " deal with dual continuous spaces on their own line
  %s/\v^  $/vimrc_imtwospacesonourownline
  " deal with dual continuous spaces on the start of their line
  %s/\v^  \ze\S/vimrc_imtwospacesonourownline
  " deal with dual continuous spaces on the end of their line
  %s/\v\S\zs  $/vimrc_imtwospacesonourownline
  " use two replace commands so that this file itself won't be contaminated
  %s/vimrc_imtwospacesonourownline/vimrc_imtwospacesonourownlineleaveusalone
  " deal with other dual continuous spaces, which is what we want
  %s/\v\S\zs  \ze\S/ /
  " put back the innocent ones
  %s/vimrc_imtwospacesonourownlineleaveusalone/  /
  " return to the previous marker
  normal `p
endfunction

command! ClearTwoContinuousSpaces silent! call ClearTwoContinuousSpacesFunction()
